<?php

include('../config.inc');
require('dummy-app.inc');

if ($_GET['action']) {
session_start();
if ( ! (session_id() && isset($_SESSION['unum'])) ) {

        $tree = make_app("Search :: Add Preference :: Invalid User");
        $app = $tree->app;
        $app->addChild('p', "Please Login Before Using This Function.");

        session_end();
        echo $tree->asXML();
        exit;

} elseif ($_GET['action'] == 'add' and 
	$_GET['VIN'] and $_GET['Plan']) {
		mysql_query("insert into Car_Interested 
			value ($_SESSION[unum], $_GET[VIN], $_GET[Plan]);") 
			or exit_404('Add preference failed.');
	exit;
} elseif ($_GET['action'] == 'save' and 
	($_GET['Make'] or $_GET['Model'])) {
	$where = assoc2query(array_map(db_escape, array_clean(array('Make' => $_GET['Make'], 'Model' => $_GET['Model']))));
	$mnum = mysql_fetch_row(mysql_query("select CarModelNum from Car_Model where $where;"));
	$mnum && mysql_query("insert into Model_Interested 
		value ($_SESSION[unum], $mnum[0]);") 
		or exit_404('Save Model failed.');
	exit;
} else exit_404('Bad action.');
}

$tree = make_app("Search");
$app = $tree->app;
$app->h2 = "Search Cars";
$app->form['action'] = 'app/search.php';
$app->form->p = 'Select a Make and Model and make your feature plan.';
$form = $app->form;
$form->div['id'] = 'search_left';
$form->div['class'] = 'layout_left';
$form->div->label[] = 'Make: ';
add_child($form->div, 'select', 
	array('name' => 'Make', 'onchange' => 'submit()'));
$_GET['Make'] or add_default_option($form->div->select, 'Select a Make');
add_car_options($form->div->select, 'Make');
$form->div->label[] = 'Model: ';
add_child($form->div, 'select', 
	array('name' => 'Model', 'onchange' => 'submit()'));
$_GET['Model'] or add_default_option($form->div->select[1], 'Select a Model');
add_car_options($form->div->select[1], 'Model');
$form->div[]['id'] = 'search_right';
$form->div[1]['class'] = 'layout_right';
$form->div[1]->label[] = 'Year: ';
add_child($form->div[1], 'select', 
	array('name' => 'Year', 'onchange' => 'submit()'));
$_GET['Year'] or add_default_option($form->div[1]->select, 'Choose a Year');
add_car_options($form->div[1]->select, 'Year');
$form->div[1]->label[] = 'Price: ';
add_child($form->div[1], 'select', 
	array('name' => 'StickerPrice', 'onchange' => 'submit()'));
$_GET['StickerPrice'] or add_default_option($form->div[1]->select[1], 'Choose a Price');
add_car_options($form->div[1]->select[1], 'StickerPrice');
if ($_GET['Make'] or $_GET['Model'] or 
	$_GET['Year'] or $_GET['StickerPrice']) {
	$form->label[] = 'Trim: ';
	add_child($form, 'select', 
		array('name' => 'VIN', 'onchange' => 'submit()'));
	add_car_options_vin($form->select, 'Trim');
}
if ($_GET['VIN']) {
	$form->label[] = 'Feature Plan: ';
	$form->addChild('br');
	add_carplan_radio($form->addChild('div'), 'Plan');
}
if (!$_GET['VIN']) {
	add_child($form, 'input', 
		array('type' => 'button', 'value' => 'Continue', 
			'onclick' => 'submit()'));
} else {
	add_child($form, 'input', 
		array('type' => 'button', 'value' => 'Add to Preference', 
			'onclick' => "call_app('search', 
			'action=add&VIN=$_GET[VIN]&Plan='+get_checked(this.previousSibling));load_app('preference')"));
}
add_child($form, 'input', 
	array('type' => 'reset', 'value' => 'Reset', 
		'onclick' => "load_app('search')"));
if ($_GET['Make'] or $_GET['Model']) {
	add_child($form, 'input', 
		array('type' => 'button', 'value' => 
			'Save the Model', 
			'onclick' => "call_app('search', 
			'action=save&Make=$_GET[Make]&Model=$_GET[Model]');load_app('preference')"));
}

echo $tree->asXML();

function add_car_options($xml, $field) {
	$ls = array();
	$avail_cars = select_car($_GET, $field);
	foreach ($avail_cars as $car) {
		$ls[$car[$field]] = $car[$field];
	}
	return add_options($xml, $ls);
}

function add_car_options_vin($xml, $field) {
	$ls = array();
	$avail_cars = select_car_vin($_GET);
	foreach ($avail_cars as $car) {
		$ls[$car['VIN']] = $car[$field];
	}
	return add_options($xml, $ls);
}

function add_carplan_radio($xml, $name) {
	$ls = array();
	$avail_cars = select_car_vin($_GET);
	foreach ($avail_cars as $car) {
		$ls[$car['Plan']] = carplan2str($car);
	}
	return add_radios($xml, $ls, $name);
}

function carplan2str($arr) {
	$str = '';
	if ($arr['Doors']) $str .= "$arr[Doors]xDoors ";
	if ($arr['AC']) $str .= "AC ";
	if ($arr['PowerWing']) $str .= "PWing ";
	if ($arr['PowerLook']) $str .= "PLock ";
	if ($arr['FourWD']) $str .= "4WD ";
	if ($arr['Color']) $str .= "$arr[Color] ";
	return $str;
}

function add_default_option($xml, $str) {
	$xml->option = $str;
	$xml->option['value'] = '';
}

function add_options($xml, $ls) {
	foreach ($ls as $k => $v) {
		$op = $xml->addChild('option', $v);
		$op['value'] = $k;
	}
	return $xml;
}

function add_radios($xml, $ls, $n) {
	foreach ($ls as $k => $v) {
		$op = $xml->addChild('input');
		$op['type'] = 'radio';
		$op['name'] = $n;
		$op['value'] = $k;
		$xml->p[] = $v;
	}
	return $xml;
}

function array_clean($arr) {
	$ls = array();
	foreach ($arr as $k => $v)
		if ($v) $ls[$k] = $v;
	return $ls;
}

function select_car_vin($arr) {
	$where = assoc2query(array_map(db_escape, array_clean($arr)));
	$res = mysql_query("select VIN, 
		CONCAT_WS(' ', Year, Make, Model, Style) as Trim, 
		Plan, Doors, AC, PowerWing, PowerLock, FourWD, Color 
		from Car_Info where $where;");
	$ls = array();
	for ($i = 0; $i < mysql_num_rows($res); $i++)
		$ls[] = mysql_fetch_assoc($res);
	return $ls;
}

function select_car($arr, $field) {
	$where = assoc2query(array_map(db_escape, array_clean($arr)));
	$res = mysql_query(sprintf("select distinct $field 
		from Car_Info %s order by $field;", 
		$where ? 'where '. $where : ''));
	$ls = array();
	for ($i = 0; $i < mysql_num_rows($res); $i++)
		$ls[] = mysql_fetch_assoc($res);
	return $ls;
}

function assoc2query($arr) {
	$ls = array();
	foreach ($arr as $k => $v)
		$ls[$k] = "$k = $v";
	return implode(' and ', $ls);
}
?>
